﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using HWND = System.IntPtr;

namespace Loong.Console
{
    public class ColorFontTextBox : RichTextBox
    {
         /// <summary>
  /// 必需的设计器变量。
  /// </summary>
  private System.ComponentModel.Container components = null;

//使用win32api：SendMessage来防止控件着色时的闪烁现象

  [DllImport("user32")] private static extern int SendMessage(HWND hwnd, int wMsg, int wParam, IntPtr lParam);
  private const int WM_SETREDRAW = 0xB;

  public ColorFontTextBox()
  {
   // 该调用是 Windows.Forms 窗体设计器所必需的。
   InitializeComponent();
   base.WordWrap=false;
   // TOD 在 InitComponent 调用后添加任何初始化

  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if( components != null )
     components.Dispose();
   }
   base.Dispose( disposing );
  }

  #region 组件设计器生成的代码
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器
  /// 修改此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
   //
   // SyntaxEditor
   //
      this.Name = "ColorFontTextBox";

  }
  #endregion



  //重写基类的OnTextChanged方法。为了提高效率，程序是对当前文本插入点所在行进行扫描，

  //以空格为分割符，判断每个单词是否为关键字，并进行着色。

  protected override void OnTextChanged(EventArgs e)
  {
      if (base.Text != "")
      {
          int selectStart = base.SelectionStart;
          int line = base.GetLineFromCharIndex(selectStart);

          string lineStr = base.Lines[line];
          int linestart = 0;
          for (int i = 0; i < line; i++)
          {
              linestart += base.Lines[i].Length + 1;
          }

          SendMessage(base.Handle, WM_SETREDRAW, 0, IntPtr.Zero);


          int index = base.SelectionStart;　　//记录修改的位置
          base.SelectAll();
          base.SelectionColor = Color.Black;
          base.SelectionFont = new Font(Font, FontStyle.Regular);
          string[] keystr = { "(*)", "|", "参数[0]", "参数[1]", "参数[2]", "参数[3]", "参数[n]" };
         // string[] keystr = { "select ", "from ", "where ", " and ", " or ", " order ", " by ", " desc ", " when ", " case ", " then ", " end ", " on ", " in ", " is ", " else ", " left ", " join ", " not ", " null " };
       
          for (int i = 0; i < keystr.Length; i++)
          {
              this.getbunch(keystr[i], base.Text);
          }
          base.Select(index, 0);　　 //返回修改的位置
          base.SelectionColor = Color.Black;




          SendMessage(base.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
          base.Refresh();
      }
      base.OnTextChanged(e);
  }

  public int getbunch(string p, string s) //给关键字上色
  {
      int cnt = 0; int M = p.Length; int N = s.Length;
      char[] ss = s.ToCharArray(), pp = p.ToCharArray();
      if (M > N) return 0;
      for (int i = 0; i < N - M + 1; i++)
      {
          int j;
          for (j = 0; j < M; j++)
          {
              if (ss[i + j] != pp[j]) break;
          }
          if (j == p.Length)
          {
              base.Select(i, p.Length);
              base.SelectionFont = new Font(Font, FontStyle.Bold);
              base.SelectionColor = color;
              cnt++;
          }
      }
      return cnt;
  }
  private bool isbold=true;
        
  public bool IsBold
  {
      get { return this.isbold; }
      set { this.isbold=value; }
  }

  private Color color=Color.Green;
  public Color SelectColor
  {
      get { return this.color; }
      set { this.color = value; }
  }
    }
}
